package servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.SellerService;
import service.UserService;
import sun.misc.BASE64Decoder;

public class SellerUploadLogo extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SellerUploadLogo() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String pic = request.getParameter("pic");
		String pic1 = request.getParameter("pic1");
		String pic2 = request.getParameter("pic2");
		String pic3 = request.getParameter("pic3");

		String sellerId = request.getParameter("sellerId");

		System.out.println("sellerId =   = " + sellerId);

		String path = request.getSession().getServletContext().getRealPath("/")
				+ "images/seller/";

		// 原图
		File file = new File(path + "seller_"+sellerId + "_src.png");
		FileOutputStream fout = null;
		fout = new FileOutputStream(file);
		fout.write(new BASE64Decoder().decodeBuffer(pic));
		fout.close();

		// 图1
		File file1 = new File(path + "seller_"+sellerId  + "_1.png");
		FileOutputStream fout1 = null;
		fout1 = new FileOutputStream(file1);
		fout1.write(new BASE64Decoder().decodeBuffer(pic1));
		fout1.close();

		// 图2
		File file2 = new File(path + "seller_"+sellerId  + "_2.png");
		FileOutputStream fout2 = null;
		fout2 = new FileOutputStream(file2);
		fout2.write(new BASE64Decoder().decodeBuffer(pic2));
		fout2.close();

		// 图3
		File file3 = new File(path + "seller_"+sellerId  + "_3.png");
		FileOutputStream fout3 = null;
		fout3 = new FileOutputStream(file3);
		fout3.write(new BASE64Decoder().decodeBuffer(pic3));
		fout3.close();

		try {
			SellerService.changeSellerLogo(sellerId,
					"images/seller/" + file1.getName());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		out.println("{\"status\":1}");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
